package com.google.gwt.sample.json.client;

import org.gwtwidgets.client.util.WindowUtils;

import com.google.gwt.sample.json.client.Header.JSONResponseTextHandler;
import com.google.gwt.sample.json.client.SearchResult.JSONCountResponseTextHandler;
import com.google.gwt.user.client.HTTPRequest;
import com.google.gwt.user.client.ui.*;
import com.google.gwt.json.client.JSONValue;
import com.google.gwt.user.client.Window;

public class CodeSearchResult extends SearchResult{
	public CodeSearchResult(boolean searchWithin){
		super(searchWithin);
	}
	public Widget makeEntryLabel(JSONValue json){
    	final String codeName = json.isObject().get("name").toString().replaceAll("\"", "");
    	final String imageLoc = json.isObject().get("image").toString().replaceAll("\"", "");
		final HorizontalPanel hp_1 = new HorizontalPanel();
    	final Label label = new Label(codeName);
    	label.setHeight("60px");
    	label.setStyleName("gwt-Result");		   
    	label.addMouseListener(new SearchResultMouseListener(label, json));
		final Image im_1 = new Image(imageLoc);
		im_1.setSize("60px", "60px");
		hp_1.add(im_1);
		hp_1.add(label);
    	return hp_1;
	}
	public EntryPanel makeEntryPanel(){return new CodeEntry();};
	public String entryName(){return "Code";};
	public void doSearch(String tbText, boolean findCount){
		if(findCount){
      	  String searchText = "/python/codesamples/count?search=" + tbText;
		  HTTPRequest.asyncGet(searchText, new JSONCountResponseTextHandler());		        	  
		}
		String offsetStr = WindowUtils.getLocation().getParameter("offset");
		if(offsetStr != null) offset = Integer.parseInt(offsetStr);
		String searchText = "/python/codesamples?search=" + tbText + "&offset=" + offset;
		HTTPRequest.asyncGet(searchText, new JSONResponseTextHandler(this, tbText, true));
	}
}
